import Axios from 'axios'
import qs from 'qs'
const option = {
  timeout: 30000,
  baseURL: process.env.VUE_APP_BASE_API,
  headers: {
    'Content-Type': 'application/x-www-form-urlencoded'
  },
  transformRequest: (data, headers) => {
    if (headers['Content-Type'].search('application/json') !== -1) {
      return JSON.stringify(data)
    } else if (headers['Content-Type'].search('multipart/form-data') !== -1) {
      return data
    } else {
      return qs.stringify(data)
    }
  }
}
const axios = Axios.create(option)
// 请求拦截器
axios.interceptors.request.use(
  (config) => {
    console.log(config.url.replace('/api', ''))
    const requestUri =
      process.env.NODE_ENV === 'development' ? config.url.replace(process.env.VUE_APP_BASE_API, '') : config.url
    const timestamp = String(+new Date())
    config.headers = { ...config.headers, timestamp, requestUri }
  },
  (error) => {
    console.log(error)
  }
)
// 添加响应拦截器
axios.interceptors.response.use((response) => {
  console.log(response)
})
export default axios
